package com.ellricky.baselib.ui.base;

import android.view.View;
import android.widget.AdapterView;

import com.andr.common.http.callback.BaseCallback;
import com.andr.common.http.callback.JOCallback;
import com.andr.common.http.util.JsonUtil;
import com.andr.common.ui.adapter.BaseRecyclerAdapter;
import com.andr.common.ui.mvp.BasePresenter;
import com.andr.common.util.EmptyUtil;
import com.andr.common.widget.refresh.RefreshFlag;
import com.ellricky.baselib.adapter.PersonAdapter;
import com.ellricky.baselib.cache.LocalCacheUtil;
import com.ellricky.baselib.entity.PersonEntity;
import com.ellricky.baselib.entity.PersonItemEntity;
import com.ellricky.baselib.entity.StaffList;
import com.ellricky.baselib.listener.PersonListListener;
import com.ellricky.baselib.ui.base.BaseRefreshFragment;
import com.ellricky.baselib.util.NewEBaseCallback;
import com.ellricky.baselib.util.NewECallback;
import com.ellricky.baselib.util.NewEListCallback;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public abstract class PersonListFragment extends BaseRefreshFragment<Void, BasePresenter<Void>, PersonEntity>
        implements PersonListListener {

    private boolean choosePage = false;
    private boolean showRateBar = true;
    private boolean showClassNum = false;
    private boolean showSex = false;

    protected boolean isChoosePage() {
        return false;
    }

    protected boolean isShowRateBar() {
        return true;
    }

    public boolean isShowClassNum() {
        return showClassNum;
    }

    public boolean isShowSex() {
        return showSex;
    }

    @Override
    public void initUIView(View view) {
        showRateBar = isShowRateBar();
        choosePage = isChoosePage();
        showClassNum = isShowClassNum();
        showSex = isShowSex();
        super.initUIView(view);
    }

    @Override
    protected void setRefreshListener() {
        rvView.init(this, RefreshFlag.REFRESH);
    }


    @Override
    protected BaseCallback getResponseCallback() {
        return null;
    }

    @Override
    protected NewEBaseCallback getResponseCallback2() {
        return new NewECallback<StaffList>(getActivity()) {

            @Override
            public void onSuccess(StaffList data, String msg) {
                dataSource.clear();
                ArrayList<StaffList.Staff> teacherList = data.getTeacherList();
                PersonEntity personEntity1 = new PersonEntity();
                personEntity1.setShortName("教师");
                List<PersonItemEntity> teacherItemList = new ArrayList<>();
                for (StaffList.Staff staff : teacherList) {
                    PersonItemEntity personItemEntity = new PersonItemEntity();
                    personItemEntity.setId(String.valueOf(staff.getId()));
                    personItemEntity.setName(String.valueOf(staff.geteName()));
                    personItemEntity.setHeadPic(String.valueOf(staff.geteHead()));
                    personItemEntity.setUserLevel(String.valueOf(staff.geteLevel()));
                    personItemEntity.setWorkStatus(String.valueOf(staff.getIsRest()));
                    personItemEntity.setCardType(String.valueOf(staff.geteTypeid()));
                    teacherItemList.add(personItemEntity);
                }
                personEntity1.setData(teacherItemList);

                ArrayList<StaffList.Staff> gzaList = data.getGzaList();
                PersonEntity personEntity2 = new PersonEntity();
                personEntity2.setShortName("学管");
                List<PersonItemEntity> GzaItemList = new ArrayList<>();
                for (StaffList.Staff staff : gzaList) {
                    PersonItemEntity personItemEntity = new PersonItemEntity();
                    personItemEntity.setId(String.valueOf(staff.getId()));
                    personItemEntity.setName(String.valueOf(staff.geteName()));
                    personItemEntity.setHeadPic(String.valueOf(staff.geteHead()));
                    personItemEntity.setUserLevel(String.valueOf(staff.geteLevel()));
                    personItemEntity.setWorkStatus(String.valueOf(staff.getIsRest()));
                    personItemEntity.setCardType(String.valueOf(staff.geteTypeid()));
                    GzaItemList.add(personItemEntity);
                }
                personEntity2.setData(GzaItemList);

                ArrayList<StaffList.Staff> saleList = data.getSaleList();
                PersonEntity personEntity3 = new PersonEntity();
                personEntity3.setShortName("课程顾问");
                List<PersonItemEntity> saleItemList = new ArrayList<>();
                for (StaffList.Staff staff : saleList) {
                    PersonItemEntity personItemEntity = new PersonItemEntity();
                    personItemEntity.setId(String.valueOf(staff.getId()));
                    personItemEntity.setName(String.valueOf(staff.geteName()));
                    personItemEntity.setHeadPic(String.valueOf(staff.geteHead()));
                    personItemEntity.setUserLevel(String.valueOf(staff.geteLevel()));
                    personItemEntity.setWorkStatus(String.valueOf(staff.getIsRest()));
                    personItemEntity.setCardType(String.valueOf(staff.geteTypeid()));
                    saleItemList.add(personItemEntity);
                }
                personEntity3.setData(saleItemList);

                ArrayList<StaffList.Staff> marketList = data.getMarketList();
                PersonEntity personEntity4 = new PersonEntity();
                personEntity4.setShortName("市场");
                List<PersonItemEntity> marketItemList = new ArrayList<>();
                for (StaffList.Staff staff : marketList) {
                    PersonItemEntity personItemEntity = new PersonItemEntity();
                    personItemEntity.setId(String.valueOf(staff.getId()));
                    personItemEntity.setName(String.valueOf(staff.geteName()));
                    personItemEntity.setHeadPic(String.valueOf(staff.geteHead()));
                    personItemEntity.setUserLevel(String.valueOf(staff.geteLevel()));
                    personItemEntity.setWorkStatus(String.valueOf(staff.getIsRest()));
                    personItemEntity.setCardType(String.valueOf(staff.geteTypeid()));
                    marketItemList.add(personItemEntity);
                }
                personEntity4.setData(marketItemList);

                dataSource.add(personEntity1);
                dataSource.add(personEntity2);
                dataSource.add(personEntity3);
                dataSource.add(personEntity4);

                rvView.onLoadFinish(dataSource, refresh);
            }
        };
    }

    @Override
    protected void initParamMap() {
        super.initParamMap();
        paramMap.put("action", "headSchoolEmpList");
        paramMap.put("eid", LocalCacheUtil.getUid());
    }

    @Override
    protected BaseRecyclerAdapter<PersonEntity> getRvAdapter() {
        PersonAdapter personAdapter = new PersonAdapter(null, this, this);
        personAdapter.setChoosePage(choosePage);
        personAdapter.setShowRateBar(showRateBar);
        personAdapter.setShowClassNum(showClassNum);
        personAdapter.setShowSex(showSex);
        adapter = personAdapter;
        return adapter;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        super.onItemClick(adapterView, view, i, l);
        // do none
    }

    @Override
    public void onClickPersonItem(PersonItemEntity personItemEntity) {

    }
}
